package com.rzjm.api.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.rzjm.api.config.RequestDataHelper;
import com.rzjm.api.dao.SurveyUserRecordDao;
import com.rzjm.api.dao.SurveyUserRecordInfoDao;
import com.rzjm.api.entity.Parameter;
import com.rzjm.api.entity.SurveyUserRecord;
import com.rzjm.api.entity.SurveyUserRecordInfo;
import com.rzjm.api.entity.User;
import com.rzjm.api.exception.TransactionalException;
import com.rzjm.api.service.ParameterService;
import com.rzjm.api.service.SurveyUserRecordInfoService;
import com.rzjm.api.service.SurveyUserRecordService;
import com.rzjm.api.util.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 问卷调查的用户填报数据Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-05
 */
@Service
public class SurveyUserRecordInfoServiceImpl extends BaseServiceImpl<SurveyUserRecordInfoDao, SurveyUserRecordInfo> implements SurveyUserRecordInfoService {

    @Resource
    private SurveyUserRecordInfoDao recordInfoMapper;
    @Resource
    private ParameterService parameterService;

    @Override
    @Transactional
    public int addRecordInfo(SurveyUserRecord surveyUserRecord, User user, String answer) {
        try{

            Map<String,Object> map = new HashMap<>();
            map.put("survey_user_record_info", surveyUserRecord.getRecordTableName());
            RequestDataHelper.setRequestData(map);

            JSONArray jsonArray = JSONUtil.parseArray(answer);
            for (int i = 0;i < jsonArray.size();i++){
                JSONObject jsonObject = (JSONObject)jsonArray.get(i);

                SurveyUserRecordInfo recordInfo = new SurveyUserRecordInfo();
                recordInfo.setRecordId(surveyUserRecord.getId());
                recordInfo.setUserId(user.getId());
                recordInfo.setQuestionId(jsonObject.getInt("question_id"));
                if(jsonObject.getInt("question_type") == 0){
                    recordInfo.setAnswer(jsonObject.getInt("check_value").toString());
                }else{
                    recordInfo.setAnswer(jsonObject.getStr("check_value"));
                }
                recordInfo.setCreateTime(new Date());
                int ret = recordInfoMapper.insert(recordInfo);
                if(ret <= 0){
                    throw new TransactionalException("recordMapper insert error");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new TransactionalException("addRecord insert error");
        }
        return 1;
    }

    @Override
    public String addNowYearRecordTable() {
        String tableName = "";
        try {
            Parameter parameter = parameterService.getParameterById(1);
            if(parameter != null){
                if(StrUtil.isNotEmpty(parameter.getRangeRecordTableName())){
                    tableName = parameter.getRangeRecordTableName();
                    System.out.println("缓存记录表名：" + tableName);
                }

                String tableNameNow = "survey_user_record_info_" + DateUtil.getYear();
                System.out.println("当前月份的表名：" + tableNameNow);

                if(!tableName.equals(tableNameNow)){
                    int isExits = recordInfoMapper.isTableExist(tableNameNow);
                    if(isExits == 0){
                        recordInfoMapper.createRecordTable(tableNameNow);
                        parameter.setSurveyRecordInfoName(tableNameNow);
                        parameterService.updateById(parameter);
                        parameterService.resetParameter(1);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tableName;
    }
}
